package com.timedomain.him.remake;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.util.AndroidRuntimeException;

import com.timedomain.him.remake.utils.AppSettings;
import com.timedomain.him.remake.utils.DB;
import com.timedomain.him.remake.utils.HLog;
import com.timedomain.him.remake.utils.SettingUtils;
import com.un4seen.bass.BASS;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;


public class Program extends Application {

    private static HashMap<String,DataPackEntry> dataPackMapping = null;
    private static List<DataPackEntry> dataPackEntryList = null;


    public static class DataPackEntry{
        public String path;

        public String hash;

        public DataPackEntry(String path, String hash) {
            this.path = path;
            this.hash = hash;
        }

    }

    public static InputStream openAsset(Context ctx,String datapackPath) throws IOException {
        return ctx.getAssets().open("data/"+getDataPack(ctx).get(datapackPath).hash);
    }

    private static synchronized void initDatapack(Context ctx){
        if(dataPackMapping != null){
            return;
        }

        Thread loadThread = new Thread(() -> {
            HashMap<String,DataPackEntry> dataPackMapping = new HashMap<>();
            List<DataPackEntry> dataPackEntryList = new ArrayList<>();
            try(InputStream inputStream = ctx.getAssets().open("files.txt")){
                try(BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))){
                    String line = null;
                    while ((line = br.readLine())!=null){
                        line = line.trim();
                        if(line.length() > 1) {
                            String[] args = line.split(Pattern.quote(" "),2);
                            String path = args[1].trim();
                            String hash = args[0].trim();
                            DataPackEntry entry = new DataPackEntry(path,hash);
                            dataPackEntryList.add(entry);
                            dataPackMapping.put(path,entry);
                        }
                    }
                }
                Program.dataPackMapping = dataPackMapping;
                Program.dataPackEntryList = dataPackEntryList;
            }catch (IOException ex){
                HLog.e("无法加载数据包：",ex.toString());
                ex.printStackTrace();
                //throw new AndroidRuntimeException("Error while loading datapack",ex);
            }
        });

        try {
            loadThread.start();
            loadThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static HashMap<String,DataPackEntry> getDataPack(Context ctx){
        initDatapack(ctx);
        return dataPackMapping;
    }

    public static List<DataPackEntry> getDataEntries(Context ctx){
        initDatapack(ctx);
        return dataPackEntryList;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        if(!BASS.BASS_Init(-1,48000,BASS.BASS_DEVICE_SPEAKERS)){
            int code = BASS.BASS_ErrorGetCode();
            HLog.e("libbass", "Cannot init bass: "+ code);
            //throw new AndroidRuntimeException("Cannot load bass library: reason code = "+code);
        }
        AppSettings.loadSettings(this);
        DB.ensureDBInited(this);
        initDatapack(this);
    }

}
